package com.bc.bcapi.service;

import com.bc.bcapi.dao.T_Authority_infoMapper;
import com.bc.bcmodel.domain.T_Authority_infoModel;
import com.bc.commoncore.util.DateUtil;
import com.bc.commoncore.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.math.BigInteger;

import com.bc.bcapi.dao.T_Role_infoMapper;
import com.bc.bcmodel.domain.T_Role_infoModel;

/**
 * @Name T_Role_infoService * @Description TODO * @Author generation * @Date 2018/8/9 15:56:31 * @Version 1.0
 **/
@Service
public class T_Role_infoService  {
    @Resource
    T_Role_infoMapper t_Role_infoMapper;

    /**
     * 保存单条数据     *      * @return
     */
    
    public int insert(T_Role_infoModel req) {
        t_Role_infoMapper.insert(req);
        insertRoleRelation(req.getMenu_ids(), req.getRole_id());
        return 1;
    }

    /**
     * 批量增加数据     *      * @return
     */
    
    public int insertBatch(List<T_Role_infoModel> req) {
        return t_Role_infoMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    
    public T_Role_infoModel findByNamedParam(T_Role_infoModel req) {
        return t_Role_infoMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */
    
    public List<T_Role_infoModel> getAllList(T_Role_infoModel req) {
        return t_Role_infoMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */
    
    public PageInfo<T_Role_infoModel> getList(T_Role_infoModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_Role_infoModel> list = t_Role_infoMapper.findByNamedParamList(req);
        return new PageInfo<T_Role_infoModel>(list);
    }

    @Autowired
    T_Authority_infoMapper t_authority_infoMapper;

    /**
     * 更新指定对象     *      * @return
     */
    
    public int update(T_Role_infoModel req) {
        //删除角色菜单关系表
//        T_Authority_infoModel t_authority_infoModel=new  T_Authority_infoModel();
//        t_authority_infoModel.setRole_id(req.getRole_id());
        t_authority_infoMapper.deleteByRoleId(req.getRole_id());
        insertRoleRelation(req.getMenu_ids(), req.getRole_id());
        return t_Role_infoMapper.update(req);
    }

    private void insertRoleRelation(String menu_ids, String role_id) {
        String[] arr = menu_ids.split(",");
        List<T_Authority_infoModel> list = new ArrayList<T_Authority_infoModel>();
        for (String item :
                arr) {
            if (StringUtils.isEmpty(item)) {
                continue;
            }
            T_Authority_infoModel model = new T_Authority_infoModel();
            model.setRole_id(role_id);
            model.setMenu_id(item);
            model.setStatus("1");
            model.setCreate_time(DateUtil.getToday());
            model.setModify_time(DateUtil.getToday());
            model.setMemo("");
            list.add(model);
        }
        t_authority_infoMapper.insertBatch(list);
    }

    /**
     * 删除指定数据     *      * @return
     */
    
    public int delete(BigInteger id) {
        return t_Role_infoMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    
    public int count(T_Role_infoModel req) {
        return t_Role_infoMapper.count(req);
    }

    
    public String getRoleNameById(String role_id) {
        T_Role_infoModel req = new T_Role_infoModel();
        req.setRole_id(role_id);
        T_Role_infoModel model = t_Role_infoMapper.findByNamedParam(req);
        return model == null ? "" : model.getRole_name();
    }
}